import request from '@/utils/request'

// 普通对话接口
export function getCompletion(params) {
    return request({
        url: '/api/v1/openai/generate',
        method: 'get',
        params: {
            model: params.model || 'Qwen/Qwen2.5-7B-Instruct',
            message: params.message || ''
        }
    })
}

// 流式对话接口
export function getStreamCompletion(params) {
    return request({
        url: '/api/v1/openai/generate_stream',
        method: 'get',
        params: {
            model: params.model || 'Qwen/Qwen2.5-7B-Instruct',
            message: params.message || ''
        },
        responseType: 'stream'
    })
}

// 带知识库的流式对话接口
export function getStreamRagCompletion(params, options = {}) {
    const baseURL = process.env.VUE_APP_BASE_API || '/dev-api';
    const url = `${baseURL}/api/v1/openai/generate_stream_rag?model=${encodeURIComponent(params.model || '')}&ragTag=${encodeURIComponent(params.ragTag || 'default')}&message=${encodeURIComponent(params.message || '')}`;
    return fetch(url, {
        method: 'GET',
        headers: {
            'Accept': 'text/event-stream',
            'Cache-Control': 'no-cache',
            'Authorization': localStorage.getItem('token') ? `Bearer ${localStorage.getItem('token')}` : ''
        },
        signal: options.signal || AbortSignal.timeout(300000) // 5分钟超时
    });
}

// 获取知识库列表
export function getKnowledgeBases() {
    return request({
        url: '/api/v1/rag/query_rag_tag_list',
        method: 'get'
    })
}

// 上传文件到知识库
export function uploadFileToKnowledgeBase(data) {
    return request({
        url: '/api/v1/rag/file/upload',
        method: 'post',
        headers: {
            'Content-Type': 'multipart/form-data'
        },
        data
    })
}


// 创建知识库
export function createKnowledgeBase(data) {
    return request({
        url: '/api/v1/kbs',
        method: 'post',
        data
    })
}

// 获取知识库详情
export function getKnowledgeBase(kbId) {
    return request({
        url: `/api/v1/kbs/${kbId}`,
        method: 'get'
    })
}

// 更新知识库
export function updateKnowledgeBase(kbId, data) {
    return request({
        url: `/api/v1/kbs/${kbId}`,
        method: 'put',
        data
    })
}

// 删除知识库
export function deleteKnowledgeBase(kbId) {
    return request({
        url: `/api/v1/kbs/${kbId}`,
        method: 'delete'
    })
}

// 获取知识库共享列表
export function getKnowledgeBaseShares(kbId) {
    return request({
        url: `/api/v1/kbs/${kbId}/shares`,
        method: 'get'
    })
}

// 添加知识库共享
export function addKnowledgeBaseShare(kbId, data) {
    return request({
        url: `/api/v1/kbs/${kbId}/shares`,
        method: 'post',
        data
    })
}

// 更新知识库共享权限
export function updateKnowledgeBaseShare(shareId, data) {
    return request({
        url: `/api/v1/kbs/shares/${shareId}`,
        method: 'put',
        data
    })
}

// 删除知识库共享
export function deleteKnowledgeBaseShare(shareId) {
    return request({
        url: `/api/v1/kbs/shares/${shareId}`,
        method: 'delete'
    })
}